﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using TencentWeiboSDK.Model;
using QWeiBoN.ViewModule;

namespace QWeiBoN.UserControler
{
    public partial class ReferenceUserControler : UserControl
    {

        #region 属性

        /// <summary>
        /// 选中处理
        /// </summary>
        public Action<User> Selected { set; get; }

        /// <summary>
        /// 时候正在加载数据
        /// </summary>
        private bool isLoading { set; get; }

        /// <summary>
        /// 用户相关操作类
        /// </summary>
        private UserListModel userModel = new UserListModel();

        #endregion

        public ReferenceUserControler()
        {
            InitializeComponent();
        }

        #region 自定义方法

        /// <summary>
        /// 得到屏幕尺寸
        /// </summary>
        private Size WindowSize
        {
            get
            {
                return App.Current.RootVisual.RenderSize;
            }
        }

        /// <summary>
        /// 显示该控件
        /// </summary>
        public void Show()
        {
            Dispatcher.BeginInvoke(() =>
            {
                UserListGrid.Width = WindowSize.Width;
                UserListGrid.Height = WindowSize.Height;
                UserKeyword.Text = string.Empty;
                UserListPopUp.IsOpen = true;
                UserListPopUp.UpdateLayout();
            });
        }

        /// <summary>
        /// 切换显示
        /// </summary>
        public void ToggleShow()
        {
            Dispatcher.BeginInvoke(() =>
            {
                UserListGrid.Width = WindowSize.Width;
                UserListGrid.Height = WindowSize.Height;
                UserKeyword.Text = string.Empty;
                UserListPopUp.IsOpen = !UserListPopUp.IsOpen;
                UserListPopUp.UpdateLayout();
            });
        }

        /// <summary>
        /// 隐藏该控件
        /// </summary>
        public void Hide()
        {
            Dispatcher.BeginInvoke(() =>
            {
                UserListPopUp.IsOpen = false;
                UserKeyword.Text = string.Empty;
                UserListPopUp.UpdateLayout();
            });
        }

        #endregion

        #region 事件处理

        /// <summary>
        /// 输入框获得焦点事件处理
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void UserKeyword_GotFocus(object sender, RoutedEventArgs e)
        {
            UserKeyword.Background = new SolidColorBrush(Color.FromArgb(255, 239, 235, 239));
            UserKeyword.BorderThickness = new Thickness(1);
            UserKeyword.BorderBrush = new SolidColorBrush(Color.FromArgb(255, 148, 150, 148)); 
        }

        /// <summary>
        /// 输入框失去焦点事件处理
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void UserKeyword_LostFocus(object sender, RoutedEventArgs e)
        {
            UserKeyword.Background = new SolidColorBrush(Color.FromArgb(255, 239, 239, 239));
            UserKeyword.BorderThickness = new Thickness(1);
            UserKeyword.BorderBrush = new SolidColorBrush(Color.FromArgb(255, 148, 150, 148)); 
        }

        /// <summary>
        /// 列表行选中事件处理
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void UserList_Tap(object sender, GestureEventArgs e)
        {
            if (null != Selected)
            {
                var tmpVal = (User)UserList.SelectedValue;
                Selected(tmpVal);
            }
        }

        /// <summary>
        /// 搜索按钮的事件处理
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void SearchButton_Click(object sender, RoutedEventArgs e)
        {
            if (isLoading)
            {
                return;
            }
            string keyword = UserKeyword.Text;
            if (!string.IsNullOrWhiteSpace(keyword))
            {
                isLoading = true;
                SearchButton.Content = "...";
                progressbar.IsIndeterminate = true;

                userModel.getUsersFromUserName(keyword, (callback) =>
                {
                    callback = callback.OrderByDescending(u => u.Isidol).Take(10).ToList();
                    Dispatcher.BeginInvoke(() =>
                    {
                        isLoading = false;
                        SearchButton.Content = "查找";
                        progressbar.IsIndeterminate = false;
                        UserList.ItemsSource = callback;
                    });
                });
            }
            else
            {
                (new Util.MessageBoxUtil()).Show("输入关键词进行查询", "出错了", this.UserListGrid);
            }
        }

        #endregion

    }
}
